/**
 * @PROJECT 
 * @DATE 2018年7月11日 下午9:19:57
 * @AUTHOR LUYU
 */
package com.kool.system.action.role;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kool.core.base.IBaseAction;
import com.kool.core.exception.AppException;
import com.kool.core.util.BeanUtils;
import com.kool.system.bean.SyRoleAutBean;
import com.kool.system.bean.io.role.SYAUTROLEXBean;
import com.kool.system.service.RoleService;

/**
 * @DESCRIBE 角色授权
 * @AUTHOR LUYU
 * @DATE 2018年7月11日 下午9:19:57
 *
 */
@Controller
public class AuthRoleAction implements IBaseAction {
	@Autowired
	private RoleService service;

	@Override
	@ResponseBody
	@RequestMapping(value = "/sys/role/resetAut", method = RequestMethod.POST)
	public JSONObject doAction(@RequestBody String sInput) throws AppException {
		JSONObject pkgIn = JSONObject.parseObject(sInput);
		JSONArray secRole = pkgIn.getJSONArray("SYROLECODEX");
		JSONObject propRole = secRole.getJSONObject(0);
		String roleCode = propRole.getString("roleCode");

		JSONArray secAut = pkgIn.getJSONArray("SYAUTROLEX");
		List<SyRoleAutBean> listIn = new ArrayList<>();
		for (int i = 0; i < secAut.size(); i++) {
			SYAUTROLEXBean io = JSON.toJavaObject(secAut.getJSONObject(i), SYAUTROLEXBean.class);
			SyRoleAutBean roleAut = (SyRoleAutBean) BeanUtils.io2Bean(io, SyRoleAutBean.class, "sra");
			listIn.add(roleAut);
		}
		service.resetAuth(roleCode, listIn);

		return null;
	}

}
